require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIArenaOpponentItem = {};
HelperFunc.SetUIEnv(UIArenaOpponentItem)
local this = UIArenaOpponentItem;

this.freeCtrl = nil;

function UIArenaOpponentItem.Init()
    --todo 应该小写 暂时先大写
    local name, packageName = getUIName(PanelNames.UIArenaOpponentItem);
    panelMgr:Push(name, packageName, this.onCreate, Config.useLocalUI);

end

function UIArenaOpponentItem.onCreate(obj)
    --obj:SetActive(false);
end

function UIArenaOpponentItem.initData(obj, data, idx)
    this.update(obj, data, idx);
end

function UIArenaOpponentItem.update(obj, data, idx)
    local self = UIArenaOpponentItem;
    if self.drawWrappers == nil then
        self.drawWrappers = {};
    end
    if self.Roles == nil then
        self.Roles = {};
    end
    if self.modelPos == nil then
        self.modelPos = {};
    end
    local imgPlayerIcon = obj:GetChild("PlayerIcon"):GetChild("PlayerIcon");
    local imgLevelIcon = obj:GetChild("Effect_Dwsg")
    local txtPowerText = obj:GetChild("PowerText")
    local lviCon = obj:GetChild("LevelIcon")
    local txtScoreText = obj:GetChild("ScoreText")
    self.modelPos[idx] = obj:GetChild("n60");
    local txtPlayerNameText = obj:GetChild("PlayerNameText")
    local imgIcon = obj:GetChild("imgIcon")
    freeCtrl = obj:GetController("freeCtrl");
    lviCon.visible = false
    --对手信息刷新
    if data.leaderId then
        local heroConfig = HeroManager.GetHeroConfigbyModelId(data.leaderId);
        if heroConfig then
            local url =  HeroManager.GetHeroDrawingRolePath(heroConfig.f_HeroID)
            local name = self.Roles[idx] and self.Roles[idx].name or nil;
            if url ~= name then


                resMgr:LoadModelPrefab(url, function(go)
                    local Role = {};
                    Role = self.Roles[idx];
                    local drawWrapper = self.drawWrappers[idx];
                    if drawWrapper == nil then
                        drawWrapper = GoWrapper.New();
                        self.drawWrappers[idx] = drawWrapper;
                    end                
                    
                    if (Role ~= nil) then
                        destroyImmediate(Role);
                        Role = nil
                    end

                    Role = newObject(go);
                    Role:SetActive(true)
                    Role:AddComponent(TCanvas)
                    Role:AddComponent(TChangeSortingOrder)
                    Role.name = url;
                    local aniObj = Role.transform:Find("Animation").gameObject;
                    Util.PlayAction(aniObj, "fidle", true, 4);
                    logicMgr.EquipManager.setWeaponSkin(aniObj, data.leaderId, false);

                    drawWrapper:SetWrapTarget(Role, true)
                    Util.Lua_SetLocalPositionXYZ(Role.transform, 0, 0, 0);
                    Util.Lua_GetLocalPositionXYZ(aniObj.transform, 0, 0, 0);
                    Util.Lua_SetLocalRotationXYZ(aniObj.transform, 0, 0, 0);
                    Util.Lua_SetLocalScaleXYZ(Role.transform, 90, 90, 1);
                    self.Roles[idx] = Role;
                    UIArenaOpponentItem.modelPos[idx]:SetNativeObject(drawWrapper)
                end)
            end
            -- imgPlayerIcon.url ="ui://HalfBodyAtlas/".. heroConfig.f_HeroBIcon;
            imgPlayerIcon.url = "ui://HalfBodyAtlas/" .. logicMgr.SkinManager.GetHeroBIconBySkinId(data.pic, data.leaderId)
        end
    end

    local gradConfig, level = logicMgr.ArenaManager.getGradCofigByScore(tostring(data.score));
    imgLevelIcon.animationName = "idle" .. level;
    imgLevelIcon.skinName = level;
    imgLevelIcon.loop = true;
    txtPowerText.text = getNumString(data.power);
    txtScoreText.text = tostring(data.score);
    txtPlayerNameText.text = data.playerName;

    imgIcon.url = "ui://ItemAtlas/" .. data.costItemIconName;

    this.battleState(obj, data.isFree);
end

function UIArenaOpponentItem.battleState(obj, isFree)
    --挑战按钮状态刷新
    if isFree then
        freeCtrl.selectedPage = "free";
    else
        freeCtrl.selectedPage = "notFree";
    end
end

function UIArenaOpponentItem.onClear()
    local self = UIArenaOpponentItem;
    if self.Roles==nil then
        return;
    end
    for k, v in pairs(self.Roles) do
        destroyImmediate(v);
    end
    table.clear(self.Roles);

end

function UIArenaOpponentItem.CacheHeros()
    local self = UIArenaOpponentItem;
    for key, value in pairs(self.Roles) do
        value.transform:SetParent(panelMgr.BattleMain.transform)
        BattleSceneCacheResouce:AddCacheObj(value.name, value)
    end
    table.clear(self.Roles)
    if self.drawWrappers then
        for key, value in pairs(self.drawWrappers) do
            value:SetWrapTarget(nil, false)
        end
    end
end

function UIArenaOpponentItem.Destroy()
    local self = UIArenaOpponentItem;

    self.modelPos = {};
    self.Roles = {};
    self.drawWrappers = {};
end

return UIArenaOpponentItem;
